<?php

/**
 * Level filter form.
 *
 * @package    elearning
 * @subpackage filter
 * @author     dvQuan
 */
class LevelFormFilter extends BaseLevelFormFilter
{
  public function configure()
  {
  
  	$this->widgetSchema['level_status'] = new sfWidgetFormChoice(array(
			'choices' => LevelPeer::getLevelStatusChoices(),
			'expanded' => true,
		));
		
	$this->validatorSchema['level_status'] = new sfValidatorChoice(array(
					'required'      => false,
					'choices'       => array_keys(LevelPeer::getLevelStatusChoices()),
			));
  	//not display 
	unset( $this['create_at'], $this['update_at'], $this['level_price'], $this['teach_sessions_per_week'], $this['prac_sessions_per_week'], $this['total_sessions'], $this['duration_in_week']);
	//display
  	$this->widgetSchema['lvName'] = new sfWidgetFormFilterInput();
	$this->validatorSchema['lvName'] = new sfValidatorPass(array('required' => false));
	$this->widgetSchema->setNameFormat('level_filters[%s]');
	$this->widgetSchema->setLabels(  
             array(  
                 'lvName'      => 'Level name',  
                 'course_id'     => 'Course name'  
             )  
         );  
	/*
	$levelI18nFilter = new LevelI18nFormFilter();
	$this->embedForm('i18nLevel', $levelI18nFilter );
	*/
  }
  /*
  public function getFields()
  {
    return array(
      'id'                => 'ForeignKey',
      'culture'           => 'Text',
      'level_name'        => 'Text',
      'level_description' => 'Text',
      'vocabulary'        => 'Text',
      'fluence'           => 'Text',
      'grammar'           => 'Text',
	  'lvName'			  => 'Text',
      'level_status' => 'Text',	  
    );
  }
*/
  public function getFields()
  {
    return array(
      'id'           => 'Number',
      'course_id'    => 'ForeignKey',
      'level_price'  => 'Number',
      'level_status' => 'ForeignKey',
      'create_at'    => 'Date',
      'update_at'    => 'Date',
	  'lvName'		 => 'Text',
      'level_status' => 'Text',
    );
  }
  public static function getValueOf($string)
  {
  	$find = "value=";
  	if( isset($string) == false )
		return false;
	$pos = stripos( $string, $find );
	if ( $pos == false )
		return false;
	//tim dau ["] mo
	$pos1 = stripos( $string, '"' , $pos);
	// khong thay dau ["] sau cum value=
	if ( $pos1 == false )
		return false;
	$pos2 = stripos( $string, '"' , $pos1 + 1);
	//Khong thay dau ["] dong ket thuc
	if ( $pos2 == false )
		return false;
	if ( $pos1 + 1 == $pos2 )
		return false;
	$value = substr ($string, $pos1 + 1, $pos2 -  $pos1 - 1 );
	return $value;
  }
  
  public function addLvNameColumnCriteria(Criteria $c, $field, $value) {
		$value = $this::getValueOf($this[$field]);
		if($value == false || $value == "")
		{
			//dosmt
		}
		else	
			//$this[$field]->getValue()
			$c->add(LevelI18nPeer::LEVEL_NAME, "%" . strtr($value, '*', '%') . "%", Criteria::LIKE);
		$course_id = $this['course_id']->getValue();
		if( isset($course_id) )
			$c->add(LevelPeer::COURSE_ID, $course_id, Criteria::EQUAL);
		$level_status = $this['level_status']->getValue();
		if( isset($level_status) )
			$c->add(LevelPeer::LEVEL_STATUS, $level_status, Criteria::EQUAL);		
		//parent::addFiltersCriteria($c);
		return $c;
	}
 /* 
 	public function addIdColumnCriteria(Criteria $c, $field, $value) {
			//$this[$field]->getValue()
			$c->add(LevelPeer::ID, $value, Criteria::EQUAL);
		//parent::addFiltersCriteria($c);
		return $c;
	}
  */
  
  
  
/*
  //First method you redefine buildQuery()
	public function buildQuery(array $valeurs)
	{
		$q = Doctrine::getTable('level')->createQuery('r');
		
		if(isset($valeurs['object1_field1']))
		{
		//$q->addWhere('r.field1 like ?, '*'.$valeurs['object1_field1'].'*');
		}
		
		if(isset($valeurs['object2_field2'])) // other object
		{
		$q->innerJoin('r.object2 o');
		$q->addWhere('');
		}
		
		return $q;
	}
	//End first method
*/

/*
  public function getFields() {
    return array_merge(array(
        'lvName'=> 'lvName',
      ), parent::getFields()
    );
  } //getFields

  protected function addlvNameColumnCriteria(Criteria $criteria, $field, $values) {

    //$values['lvName'] contains the field value, use it to change $criteria object
	
    // es. $criteria->add ('column', $values['text'])
	//$criteria->add ('level_name', $values['lvName']);
	///$criteria->addJoin('level_i18n') ;//r is default alias
	//$criteria->add("level_name",$values['lvName'],Criteria::LIKE);
	
	//$criteria->addJoin(TableClassNamePeer::ID, TableClassNameI18nPeer::ID);
    //$criterion = $criteria->getNewCriterion(TableClassNameI18nPeer::CULTURE, $culture);
    //$criterion->addOr($criteria->getNewCriterion(TableClassNameI18nPeer::CULTURE, $default_culture));
    //$criteria->add($criterion);
  }//addTitleColumnCriteria
*/
}
